﻿@model List<HZYProject.Domain.Course>
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">试卷名称</label>
                <div class="layui-input-inline">
                    <input type="text" id="title" name="title" placeholder="" class="layui-input">
                </div>
                <label class="layui-form-label">所属课程</label>
                <div class="layui-input-inline">
                    <select id="CourseID" name="CourseID">
                        <option value="">--全部--</option>
                        @foreach (var item in Model)
                        {
                            <option value="@item.CourseID">@item.Title</option>
                        }
                    </select>
                </div>
                <label class="layui-form-label">试卷状态</label>
                <div class="layui-input-inline">
                    <select id="status" name="status">
                        <option value="">--全部--</option>
                        <option value="1">组卷中</option>
                        <option value="2">正常</option>
                        <option value="3">停用</option>
                    </select>
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>

<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
    @*<button class="pear-btn pear-btn-sm" lay-event="auto"><i class="layui-icon layui-icon-set"></i>自</button>*@
    @*<button class="pear-btn pear-btn-sm" lay-event="manual"><i class="layui-icon layui-icon-set"></i>手</button>*@
    <button class="pear-btn pear-btn-sm" id="more_{{d.testPaperID}}"><i class="layui-icon layui-icon-triangle-d"></i></button>
</script>

<script type="text/html" id="user-status">
    {{#if (d.status == 1) { }}
    <span>组卷中</span>
    {{# }else if(d.status == 2){ }}
    <span>正常</span>
    {{# }else if(d.status == 3){ }}
    <span>停用</span>
    {{# } }}
</script>

@section script{
    <script>
        layui.use(['table', 'form', 'jquery', 'dropdown'], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let dropdown = layui.dropdown;

            let MODULE_PATH = "/TestPaper";

            let cols = [
                [{
                    type: 'checkbox'
                },
                {
                    title: '序号',
                    type: 'numbers',
                    align: 'left'
                },
                {
                    title: '试卷编号',
                    field: 'testPaperID',
                    align: 'left'
                },
                {
                    title: '课程编号',
                    field: 'courseID',
                    align: 'left'
                },
                {
                    title: '试卷名称',
                    field: 'title',
                    align: 'left'
                },
                {
                    title: '所属课程',
                    field: 'courseName',
                    align: 'left'
                },
                {
                    title: '试题数量',
                    field: 'qCount',
                    align: 'left'
                },
                {
                    title: '试卷描述',
                    field: 'content',
                    align: 'left'
                },
                {
                    title: '试卷状态',
                    field: 'status',
                    align: 'left',
                    templet: '#user-status'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'left',
                    width: 160,
                    fixed: 'right'
                }]
            ]

            table.render({
                elem: '#user-table',
                url: '/TestPaper',
                method: 'post',
                page: true,
                cols: cols,
                skin: 'line',
                toolbar: '#user-toolbar',
                defaultToolbar: [{
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
                , done: function (res, curr, count) {
                    $("[data-field='testPaperID']").css('display', 'none');
                    $("[data-field='courseID']").css('display', 'none');
                    for (var i = 0; i < res.data.length; i++) {
                        dropdown.render({
                            elem: '#more_' + res.data[i].testPaperID
                            , data: [{
                                title: '自动组卷'
                                , testId: res.data[i].testPaperID
                                , id: 100
                            }, {
                                title: '手动组卷'
                                , testId: res.data[i].testPaperID
                                , id: 101
                            }]
                            , id: '#more_' + res.data[i].testPaperID
                            , click: function (obj) {
                                //layer.tips('点击了：' + obj.title, this.elem, { tips: [1, '#5FB878'] })
                                if (obj.id == 100)
                                    window.auto(obj);
                                else if (obj.id == 101)
                                    window.manual(obj);
                            }
                        });
                    }
                }
            });

            table.on('tool(user-table)', function (obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                } else if (obj.event === 'auto') {
                    window.auto(obj);
                } else if (obj.event === 'manual') {
                    window.manual(obj);
                }
            });

            table.on('toolbar(user-table)', function (obj) {
                if (obj.event === 'add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                }
            });

            form.on('submit(user-query)', function (data) {
                console.log(data.field);
                table.reload('user-table', {
                    where: data.field
                })
                return false;
            });

            window.add = function () {
                layer.open({
                    type: 2,
                    title: '新增',
                    shadeClose: true,
                    shade: 0.1,
                    area: ['500px', '350px'],
                    content: MODULE_PATH + '/add'
                });
            }

            window.edit = function (obj) {
                layer.open({
                    type: 2,
                    title: '修改',
                    shadeClose: true,
                    shade: 0.1,
                    area: ['500px', '350px'],
                    content: MODULE_PATH + '/edit?id=' + obj.data.testPaperID
                });
            }

            window.manual = function (obj) {
                layer.open({
                    type: 2,
                    title: '手动组卷',
                    offset: 'auto',
                    shade: 0.1,
                    maxmin: true,
                    anim: 1,
                    area: ['500px', '350px'],
                    content: MODULE_PATH + '/manual?id=' + obj.testId
                });
            }

            window.remove = function (obj) {
                layer.confirm('确定要删除该试卷吗？如删除，将删除和试卷相关的所有数据。请谨慎操作。', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: MODULE_PATH + "/remove/" + obj.data['testPaperID'],
                        method: 'post',
                        dataType: 'json',
                        type: 'delete',
                        success: function (result) {
                            layer.close(loading);
                            if (result.success) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    obj.del();
                                    window.location.reload();
                                });
                            } else {
                                layer.msg("删除失败：" + result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.auto = function (obj) {
                if (obj.status != 2) {
                    layer.confirm('自动组卷会清空试卷中已有题目,确认进行此操作么？', {
                        icon: 3,
                        title: '提示'
                    }, function (index) {
                        layer.close(index);
                        let loading = layer.load();
                        $.ajax({
                            url: MODULE_PATH + "/auto/" + obj.testId,
                            type: 'Post',
                            dataType: 'json',
                            success: function (result) {
                                layer.close(loading);
                                if (result.Error) {
                                    layer.msg(result.Msg, { icon: 2 });
                                    return;
                                }
                                layer.msg('信息组卷成功,本次组卷共导入试题' + result.count + '道', { icon: 1, time: 1000 });
                                window.location.reload();
                            },
                            error: function () {
                                layer.msg('抱歉，服务器繁忙，请稍后再试！', { icon: 5 });
                            }
                        });
                    });
                }
                else {
                    layer.alert('请先将试卷状态修改为【组卷中】再进行此操作', { icon: 5 });
                }
            }

            window.batchRemove = function (obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                let ids = "";
                for (let i = 0; i < data.length; i++) {
                    ids += data[i].userId + ",";
                }
                ids = ids.substr(0, ids.length - 1);
                layer.confirm('确定要删除这些用户', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: MODULE_PATH + "batchRemove/" + ids,
                        dataType: 'json',
                        type: 'delete',
                        success: function (result) {
                            layer.close(loading);
                            if (result.success) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    table.reload('user-table');
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.refresh = function (param) {
                table.reload('user-table');
            }

        })
    </script>
}